<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>用户管理<small>菜单管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>用户管理</el-breadcrumb-item>
                    <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="菜单名称" v-show="showQuery" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                        <el-button @click="findPage()" v-show="showQuery" class="dalfBut">查询</el-button>
                        <el-button type="primary" v-show="showAdd" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                        <el-table-column prop="linkUrl" label="链接地址" align="center"></el-table-column>
                        <el-table-column prop="path" label="路径" align="center"></el-table-column>
                        <el-table-column prop="priority" label="优先权" align="center"></el-table-column>
                        <el-table-column prop="icon" label="图标" align="center"></el-table-column>
                        <el-table-column prop="description" label="描述" align="center"></el-table-column>
                        <!--<el-table-column prop="parentMenuId" label="父菜单编号" align="center"></el-table-column>-->
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" v-show="showEdit" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" v-show="showDelete" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <!--:visible.sync="dialogFormVisible"-->
                        <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                            <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="菜单名称" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="链接地址" prop="linkUrl">
                                            <el-input v-model="formData.linkUrl"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="路径" prop="path">
                                            <el-input v-model="formData.path"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="优先权" prop="priority">
                                            <el-input v-model="formData.priority"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>

                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="图标">
                                            <el-input v-model="formData.icon"/>
                                        </el-form-item>
                                    </el-col>
                                    &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
                                    <el-select v-model="formData.parentMenuId" placeholder="父菜单编号">
                                        <el-option
                                                v-for="item in options"
                                                :key="item.id"
                                                :label="item.name"
                                                :value="item.id">
                                        </el-option>
                                    </el-select>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="描述">
                                            <el-input v-model="formData.description" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>

                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                            <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="菜单名称" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="链接地址" prop="linkUrl">
                                            <el-input v-model="formData.linkUrl"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="路径" prop="path">
                                            <el-input v-model="formData.path"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="优先权" prop="priority">
                                            <el-input v-model="formData.priority"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>

                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="图标">
                                            <el-input v-model="formData.icon"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
                                    <el-select v-model="formData.parentMenuId" placeholder="父菜单编号">
                                        <el-option
                                                v-for="item in options"
                                                :key="item.id"
                                                :label="item.name"
                                                :value="item.id">
                                        </el-option>
                                    </el-select>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="描述">
                                            <el-input v-model="formData.description" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>

                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                pagination: {//分页相关模型数据
					  currentPage: 1,//当前页码
					  pageSize:10,//每页显示的记录数
					  total:0,//总记录数
					  queryString:null//查询条件
				},
				dataList: [],//当前页要展示的分页列表数据
                formData: {},//表单数据
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit:false,//编辑表单是否可见
                rules: {//校验规则
                    name: [{ required: true, message: '菜单名称为必填项', trigger: 'blur' }]
                },
                showQuery:false,
                showAdd:false,
                showEdit:false,
                showDelete:false,
                //用来接收父菜单编号的回显
                options:{}
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                //异步加载检查数据
                this.findPage();
                        //从页面获取数据
                var list = JSON.parse(window.sessionStorage.getItem("permissions"));

                                //检查查询功能
                                for ( let permission of list){
                                    if (permission == 'MENU_QUERY') {
                                        this.showQuery=true;
                                    }
                                };
                                //检查新增功能
                                for ( let permission of list){
                                    if (permission == 'MENU_ADD') {
                                        this.showAdd=true;
                                    }
                                };
                                //检查编辑功能
                                for ( let permission of list){
                                    if (permission == 'MENU_EDIT') {
                                        this.showEdit=true;
                                    }
                                };
                                //        检查删除功能
                                for ( let permission of list){
                                    if (permission == 'MENU_DELETE') {
                                        this.showDelete=true;
                                    }
                                };
            },
            methods: {
                //编辑
                handleEdit() {
                    //提交修改的检查项数据 dataEditForm
                    //校验
                    this.$refs["dataEditForm"].validate((valid) => {
                        if (valid) {
                            //3..发送新增检查项请求  checkItem对象即可
                            //alert(JSON.stringify(this.formData));
                            axios.post('/menu/edit.do',this.formData).then(res=>{
                                if(res.data.flag){
                                   // this.$message.success(res.data.message)
                                    // 弹出结果提示
                                    this.$notify({
                                        title:'成功',
                                        message: res.data.message,
                                        type: 'success'
                                    });
                                //关闭弹出
                                this.dialogFormVisible4Edit = false;
                                this.findPage();
                            }else{
                                this.formData.parentMenuId=null
                               // this.$message.error(res.data.message)
                                    // 弹出结果提示
                                    this.$notify({
                                        title:'失败',
                                        message: res.data.message,
                                        type: 'error'
                                    });
                            }
                        })}
                    });
                },
                //添加
                handleAdd () {
                    this.$refs["dataAddForm"].validate((valid) => {
                        if (valid) {
                            //2.关闭弹出
                            this.dialogFormVisible = false;
                            //3..发送新增检查项请求
                            axios.post('/menu/add.do',this.formData).then(res=>{
                                //成功回显数据
                                if(res.data.flag){
                                //提示成功
                                    // 弹出结果提示
                                    this.$notify({
                                        title: res.data.flag?'成功':'失败',
                                        message: res.data.message,
                                        type: res.data.flag?'success':'error'
                                    });
                                //跳转重新查询
                                this.findPage();
                            }
                            //失败错误提示
                        else
                            {
                                // this.$message.error(res.data.message);
                                // 弹出结果提示
                                this.$notify({
                                    title:'失败',
                                    message: res.data.message,
                                    type: 'error'
                                });
                            }
                        })
                        }
                    });

                },
                //分页查询
                findPage() {
                    //根据用户名查询他的权限
                    var queryPageBean =
                        {
                            currentPage:this.pagination.currentPage, //当前页码
                            pageSize:this.pagination.pageSize, //当前页显示条数
                            queryString:this.pagination.queryString//查询条件
                        }
                    ///分页查询
                    axios.post('/menu/findPage.do',queryPageBean).then(res=>{
                        //PageResult
                        this.pagination.total = res.data.total; //响应总记录数
                        this.dataList = res.data.rows; //响应的数据
                    })
                },
                // 重置表单
                resetForm() {
                    this.formData = {};
                },
                // 弹出添加窗口
                handleCreate() {
                    //查询新增窗口的父菜单编号和名称
                    axios.get('/menu/findNameAndId.do').then(res=>{
                        this.options=res.data;
                    })
                    //弹出窗口
                    this.dialogFormVisible = true;
                    //重置窗口
                    this.resetForm();
                },
                // 弹出编辑窗口
                handleUpdate(row) {//alert(JSON.stringify(row));
                    //查询新增窗口的父菜单编号和名称
                    axios.get('/menu/findNameAndId.do').then(res=>{
                        this.options=res.data;
                })
                    axios.get('/menu/findMenuById.do?id='+row.id).then(res=>{
                        // 弹出结果提示
                        this.$notify({
                            title: res.data.flag?'成功':'失败',
                            message: res.data.message,
                            type: res.data.flag?'success':'error'
                        });
                    this.formData = res.data.data;//将后台的数据给页面fromData 数据双向绑定 v-model指定
                })
                    this.dialogFormVisible4Edit = true;

                },
                //切换页码
                handleCurrentChange(currentPage) {
                    //重新设置页码
                    this.pagination.currentPage = currentPage;
                    //重新查询就可以得到数据
                    this.findPage();
                },
                showMessage(r){
                    //Error: Request failed with status code 403
                    if(r == 'Error: Request failed with status code 403'){
                        this.$message.error("权限不足");
                    }
                    else {
                        this.$message.error("异常系统，请联系管理员");
                    }
                },
                // 删除
                handleDelete(row) { //对象转字符串
                    this.$confirm('确认要删除检查项吗, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        //发送删除请求
                        axios.get('/menu/deleteById.do?id='+row.id).then(res=>{
                            // 弹出结果提示
                            this.$notify({
                                title: res.data.flag?'成功':'失败',
                                message: res.data.message,
                                type: res.data.flag?'success':'error'
                            });
                    //重新加载数据
                    this.findPage();
                }).catch(error=>{
                        this.showMessage(error)
                })

                }).catch(() => {
                        // 弹出结果提示
                        this.$notify({
                            title:'取消',
                            message: '已取消删除',
                            type: 'info'
                        });
                });
                }
            }
        });

    </script>
</html>